package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.supervision.PlanDict;
import com.example.mapp.PlanDictMapp;
import com.example.service.PlanDictService;
import com.example.util.ExcelUtil;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2018/8/9.
 */
@Service
public class PlanDictServiceImpl implements PlanDictService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(PlanDictServiceImpl.class);

    @Autowired
    PlanDictMapp planDictMapp;


    @Override
    public Map<String, Object> query(PlanDict map) {
        Map<String, Object> map1 = new HashMap<>();
        try {
            if (null != (map.getCurrentPage()+"") && null != (map.getPageSize()+"")) {
                Page<PlanDict> page = PageHelper.startPage(map.getCurrentPage(),
                        map.getPageSize());
                List<PlanDict> list = planDictMapp.query(map);
                map1.put("list", list);
                map1.put("total", page.getTotal());
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
        return ResultUtil.returnSuccess("成功", map1);
    }

    @Override
    public Map<String, Object> add(PlanDict map) {

        try{
            int i3 = planDictMapp.insert(map);
            if(i3>=0){
                return ResultUtil.returnSuccess("新增成功", null);
            }else{
                return ResultUtil.returnError("新增失败", null);
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("新增失败", null);
        }

    }

    @Override
    public Map<String, Object> inExcel(MultipartFile file) {
        try {
            List<List<Object>> list = ExcelUtil.getListByExcel(file.getInputStream(), file.getOriginalFilename());
            //获取导入的文件
            if (list != null && list.size() > 0) {
                for (int i = 1; i < list.size(); i++) {
                    List listObject = list.get(i);
                    if (listObject != null && listObject.size() > 0) {

                    }
                }
            }
            return ResultUtil.returnSuccess("导入成功", null);
        }catch(Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("导入失败", null);
        }
    }

    @Override
    public Map<String, Object> delete(Map map) {
        try{
            int i1 = planDictMapp.delete(map.get("id")+"");
            if(i1>=0){
                return ResultUtil.returnSuccess("删除成功", null);
            }else{
                return ResultUtil.returnError("删除失败", null);
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("删除失败", null);
        }
    }

    @Override
    public Map<String, Object> update(Map map) {
        try{
            int i2 = planDictMapp.update(map);
            if(i2>=0){
                return ResultUtil.returnSuccess("修改成功", null);
            }else{
                return ResultUtil.returnError("修改失败", null);
            }

        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultUtil.returnError("修改失败", null);
        }
    }
}
